package com.ctshk.sapp.order.custom.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderDiscountListDTO;
import com.ctshk.rpc.order.custom.req.order.*;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderDiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 折扣记录 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@Api(value="折扣", tags="折扣")
@RestController
@Slf4j
@RequestMapping("/customized-order-discount")
public class CustomizedOrderDiscountController extends BaseController {

    @DubboReference
    private ICustomizedOrderDiscountService orderDiscountService;


    @ApiOperation(value = "列表", notes = "折扣记录列表（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "折扣记录列表", operDesc = "折扣记录列表")
    @PostMapping("/list")
    public Result<PageResponse<CustomOrderDiscountListDTO>> list(@RequestBody @Valid CustomOrderIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return Result.success(orderDiscountService.list(req));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "折扣", notes = "使用折扣（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "收款管理-折扣-提交審批", operDesc = "收款管理-折扣-提交審批")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid CustomOrderDiscountAddReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.add(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "编辑折扣", notes = "编辑折扣（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "编辑折扣", operDesc = "编辑折扣")
    @PostMapping("/edit")
    public Result edit(@RequestBody @Valid CustomOrderDiscountEditReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.edit(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "删除折扣", notes = "删除折扣（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.DELETE, operTitle = "折扣記錄-删除", operDesc = "折扣記錄-删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Valid CustomOrderDiscountIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.delete(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "提交審批", notes = "提交審批（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "提交審批", operDesc = "提交審批")
    @PostMapping("/pass")
    public Result pass(@RequestBody @Valid CustomOrderDiscountAddReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.approval(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "審批", notes = "審批（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "審批", operDesc = "審批")
    @PostMapping("/approval")
    public Result passApproval(@RequestBody @Valid CustomOrderDiscountApprovalReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.passApproval(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "撤回", notes = "撤回（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "審批通过", operDesc = "審批通过")
    @PostMapping("/withdraw")
    public Result withdraw(@RequestBody @Valid CustomOrderDiscountIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderDiscountService.withdraw(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

}
